package com.example.demo.domain.platform;

import com.example.demo.domain.application.Application;
import com.example.demo.domain.platform.repository.PlatformReleaseRepository;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.persistence.*;

/**
 * 应用的发布信息
 *
 * @author yingjianhua
 */
@Entity
@Getter
@Configuration
public class PlatformRelease {

    private static PlatformReleaseRepository repository;

    @Autowired
    public void setReleaseRepository(PlatformReleaseRepository repository) {
        PlatformRelease.repository = repository;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    private Application application;

    @Enumerated(EnumType.STRING)
    private Platform platform;

    @Enumerated(EnumType.STRING)
    private ReleaseType releaseType;

    private String code;

    private Boolean released;

    protected PlatformRelease() {
    }

    public PlatformRelease(Application application, Platform platform, ReleaseType releaseType, String code, boolean released) {
        this.application = application;
        this.platform = platform;
        this.releaseType = releaseType;
        this.code = code;
        this.released = released;
    }

    /**
     * 获取下架状态的发布信息
     */
    public void remove() {
        if (this.released) {
            this.released = false;
        }
    }

    /**
     * 获取发布状态的发布信息
     */
    public void release() {
        if (!this.released) {
            this.released = true;
        }
    }

    /**
     * 发布方式
     */
    public enum ReleaseType {
        /**
         * 平台内嵌
         */
        nest,
        /**
         * 链接跳转
         */
        url,
        /**
         * 授权登录
         */
        auth,
        ;
    }
}
